import express, { Express } from "express";
import socket from "socket.io";
import http from "http";
import socketFn from "./socket";
import { User } from "./type";
import serverConfig from "./config";

const { PORT, HOST } = serverConfig;
const app: Express = express();
const server: http.Server = http.createServer(app);

export const io: socket.Server = socket(server);
export interface SID{
    [socketId: string]: User;
};
export let users: SID = {};         // 用用户 id 映射 socket

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static('build'));

// 需要注意的是 socket.join 是个异步方法
io.on("connection", (socket) => {
    socketFn(socket);
});

server.listen(PORT, HOST, () => {
    console.log(`Server is running: http://${HOST}:${PORT}`);
});
